package com.starbaba.apicount.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.starbaba.apicount.model.StatApiCountModel;
import com.starbaba.apicount.service.StatApiCountService;
import com.starbaba.apicount.service.impl.StatApiCountServiceImpl;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class StatApiCountAction extends BaseAction {

	/**
	 * api订单统计
	 */
	private static final long serialVersionUID = 1L;
	
	StatApiCountService apiCountService=new StatApiCountServiceImpl();
	
	private String fileName;
	private InputStream input;
	
	public String getFileName() {
		return fileName;
	}

	public InputStream getInput() {
		return input;
	}
	
	/**
	 * 到列表首页	
	 * @return
	 */
	public String showpage(){
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		apiCountService.topage(paramsMap);
		return ActionSupport.SUCCESS;
	}
	
	
	/**
	 * api订单统计列表
	 */
	
	public void getApiCountList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		try {
			List <StatApiCountModel>list = apiCountService.getApiCountList(pageRequest, pageResponse, paramsMap);
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			LogService.error(e);
		}
	}
	
	
	/**
	 * api订单统计列表导出
	 */
	
	public String exportDetailCVS() throws UnsupportedEncodingException {
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);	
		try {
			pageRequest.setIDisplayStart(-1);
			pageRequest.setIDisplayLength(-1);
			List<StatApiCountModel> list = apiCountService.getApiCountList(pageRequest, pageResponse, paramsMap);
			String value = apiCountService.exportDetailCVS(list);
			input = new ByteArrayInputStream(value.getBytes("GBK"));
			fileName = System.currentTimeMillis() + ".csv";
		} catch (Exception e) {
		LogService.error(e);
		}
		return ActionSupport.SUCCESS;
	}
}
